package leetcode.code0341;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//执行用时：2 ms, 在所有 Java 提交中击败了100.00%的用户
//
//内存消耗：44.2 MB, 在所有 Java 提交中击败了5.01%的用户
//
//通过测试用例：43 / 43
public class NestedIterator implements Iterator<Integer> {

	public interface NestedInteger {

		// @return true if this NestedInteger holds a single integer, rather than a
		// nested list.
		public boolean isInteger();

		// @return the single integer that this NestedInteger holds, if it holds a
		// single integer
		// Return null if this NestedInteger holds a nested list
		public Integer getInteger();

		// @return the nested list that this NestedInteger holds, if it holds a nested
		// list
		// Return empty list if this NestedInteger holds a single integer
		public List<NestedInteger> getList();
	}

	List<Integer> list;
	Iterator<Integer> it;

	public NestedIterator(List<NestedInteger> nestedList) {
		list = new ArrayList<>();
		this.init(nestedList);
		it = list.iterator();
	}

	private void init(List<NestedInteger> nestedList) {
		for (NestedInteger ni : nestedList) {
			if (ni.isInteger()) {
				list.add(ni.getInteger());
			} else {
				this.init(ni.getList());
			}
		}
	}

	@Override
	public Integer next() {
		return it.next();
	}

	@Override
	public boolean hasNext() {
		return it.hasNext();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
